# Copyright (C) 2013-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

load_lib "trace-support.exp"
load_lib dwarf.exp

if {![dwarf2_support]} {
    return 0
}

standard_testfile .c

set asm_file "${testfile}-dbg.s"
set opts {}

if  { [gdb_compile ${srcdir}/${subdir}/${srcfile} ${binfile}1.o \
	   object {nodebug}] != "" } {
    return -1
}

Dwarf::assemble $asm_file {
    declare_labels uchar_label struct_s_label foo_label struct_t_label bar_label

    cu {} {
	compile_unit {{language @DW_LANG_C}} {
	    uchar_label: DW_TAG_base_type {
		{name "unsigned char"}
		{byte_size 1 DW_FORM_sdata}
		{encoding @DW_ATE_unsigned_char}
	    }

	    struct_s_label: DW_TAG_structure_type {
		{name s}
		{byte_size 3 DW_FORM_sdata}
		{decl_file 1 DW_FORM_sdata}
		{decl_line 1 DW_FORM_sdata}
	    } {
		DW_TAG_member {
		    {name a}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 0
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name b}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name c}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 2
		    } SPECIAL_expr}
		}
	    }

	    struct_t_label: DW_TAG_structure_type {
		{name t}
		{byte_size 3 DW_FORM_sdata}
		{decl_file 1 DW_FORM_sdata}
		{decl_line 1 DW_FORM_sdata}
	    } {
		DW_TAG_member {
		    {name a}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 0
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name b}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 7 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name c}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 6 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name d}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 5 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name e}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 4 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name f}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 3 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name g}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 2 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name h}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 1 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name i}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 0 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name j}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 2
		    } SPECIAL_expr}
		}
	    }

	    DW_TAG_subprogram {
		{name foo}
		{decl_file 1 sdata}
		{low_pc foo addr}
		{high_pc foo_end_lbl addr}
	    } {
		DW_TAG_formal_parameter {
		    {type :$struct_s_label}
		    {name x}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 2
			DW_OP_reg0
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_s_label}
		    {name y}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_reg0
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_s_label}
		    {name z}
		    {location {
			DW_OP_reg0
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 2
		    } SPECIAL_expr}
		}
	    }


	    DW_TAG_subprogram {
		{name bar}
		{decl_file 1 sdata}
		{low_pc bar addr}
		{high_pc bar_end_lbl addr}
	    } {
		DW_TAG_formal_parameter {
		    {type :$struct_t_label}
		    {name x}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_reg0
			DW_OP_bit_piece 1 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 7 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_t_label}
		    {name y}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 3 0
			DW_OP_reg0
			DW_OP_bit_piece 1 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 4 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_t_label}
		    {name z}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 7 0
			DW_OP_reg0
			DW_OP_bit_piece 1 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
	    }

	}
    }
}

if  { [gdb_compile ${asm_file} ${binfile}2.o object {nodebug}] != "" } {
    return -1
}

if  { [gdb_compile [list ${binfile}1.o ${binfile}2.o] ${binfile} \
	   executable {}] != "" } {
    return -1
}

clean_restart ${testfile}

if ![runto_main] {
    return -1
}

if ![gdb_target_supports_trace] {
    unsupported "target does not support trace"
    return -1
}

gdb_breakpoint "end"

with_test_prefix "tracing foo" {
    gdb_test "trace foo" ".*"
    gdb_test_no_output "tstart"
    gdb_test "continue" "Continuing\\.\[ \r\n\]+Breakpoint.*"
    gdb_test_no_output "tstop"

    gdb_test "tfind 0" "Found trace frame 0, tracepoint .*"
    gdb_test "p/d x" "\\\$${decimal} = {a = 0, b = 0, c = <unavailable>}"
    gdb_test "p/d y" "\\\$${decimal} = {a = 0, b = <unavailable>, c = 0}"
    gdb_test "p/d z" "\\\$${decimal} = {a = <unavailable>, b = 0, c = 0}"

    gdb_test "tfind none" "No longer looking at any trace frame.*"
}

with_test_prefix "tracing bar" {
    gdb_test "trace bar" ".*"
    gdb_test_no_output "tstart"
    gdb_test "continue" "Continuing\\.\[ \r\n\]+Breakpoint.*"
    gdb_test_no_output "tstop"

    gdb_test "tfind 0" "Found trace frame 0, tracepoint .*"
    gdb_test "p/d x" "\\\$${decimal} = {a = 0, b = <unavailable>, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0}"
    gdb_test "p/d y" "\\\$${decimal} = {a = 0, b = 0, c = 0, d = 0, e = <unavailable>, f = 0, g = 0, h = 0, i = 0, j = 0}"
    gdb_test "p/d z" "\\\$${decimal} = {a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = <unavailable>, j = 0}"

    gdb_test "tfind none" "No longer looking at any trace frame.*"
}
